﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Collection.Core;
using Collection.Entities;
using Collection.Core.Processing;

namespace Collection.Platform
{
    /// <summary>
    /// 采集监视器对象
    /// </summary>
    public class CollectMonitor
    {
        public CollectMonitor()
        {
            //类目信息的更新监视
            ServiceFactory.CatalogService.UpdateToDataBase += this.OverseeUpateCatalog;

            //类目信息的插入监视
            ServiceFactory.CatalogService.InsertToDatabase += this.OverseeInsertCatalog;

            //类目根属性的插入监视
            ServiceFactory.CatalogService.InsertPropertyToDatabase += this.OverseeInsertProperty;

            //类目根属性的更新监视
            ServiceFactory.CatalogService.UpdatePropertyToDatabase += this.OverseeUpdateProperty;

            //属性值数据的插入监视
            ServiceFactory.CatalogService.InsertPropertyValueToDatabase += this.OverseeInsertPropertyValue;

            //属性值数据的更新监视
            ServiceFactory.CatalogService.UpdatePropertyValueToDataBase += this.OverseeUpdatePropertyValue;

            //商品信息的插入监视
            ServiceFactory.MerchandiseService.InsertToDatabase += this.OverseeInsertProduct;

            //商品信息的更新监视
            ServiceFactory.MerchandiseService.UpdateToDataBase += this.OverseeUpdateProduct;
        }

        #region 类目数据信息采集的监视

        public void OverseeInsertCatalog(Catalog catalog)
        {
            this.Oversee(catalog, "操作：插入");
        }

        public void OverseeUpateCatalog(Catalog catalog)
        {
            this.Oversee(catalog, "操作：更新");
        }

        public void Oversee(Catalog catalog, String action)
        {
            Console.WriteLine(action);
            Console.WriteLine("编号：{0}", catalog.Id);
            Console.WriteLine("名称：{0}", catalog.Name);
            this.BreakRow();
        }

        #endregion

        #region 类目根属性信息采集的监视

        public void OverseeInsertProperty(Property property)
        {
            this.Oversee(property, "操作：插入");
        }

        public void OverseeUpdateProperty(Property property)
        {
            this.Oversee(property, "操作：更新");
        }

        public void Oversee(Property property, String action)
        {
            Console.WriteLine(action);
            var category = ServiceFactory.CatalogService.GetCatalogById(property.CatalogId);
            Console.WriteLine("所属分类：{0}", category.Name);
            Console.WriteLine("属性编号：{0}", property.Id);
            Console.WriteLine("属性名称：{0}", property.Name);
            this.BreakRow();
        }

        #endregion

        #region 属性值数据信息采集的监视

        /// <summary>
        /// 属性值插入操作进行监视
        /// </summary>
        /// <param name="propertyValue">属性值对象</param>
        public void OverseeInsertPropertyValue(PropertyValue propertyValue)
        {
            this.Oversee(propertyValue, "操作：插入");
        }

        /// <summary>
        /// 属性值插入操作进行监视
        /// </summary>
        /// <param name="propertyValue">属性值对象</param>
        public void OverseeUpdatePropertyValue(PropertyValue propertyValue)
        {
            this.Oversee(propertyValue, "操作：更新");
        }

        /// <summary>
        /// 属性值信息采集的监视
        /// </summary>
        /// <param name="propertyValue">属性值数据</param>
        /// <param name="action">动作（更新 or 插入）</param>
        public void Oversee(PropertyValue propertyValue, String action)
        {
            Console.WriteLine(action);
            var category = ServiceFactory.CatalogService.GetCatalogById(propertyValue.CatalogId);
            var property = ServiceFactory.CatalogService.GetProperty(propertyValue.CatalogId, propertyValue.PropertyId);
            Console.WriteLine("所属类目：{0}", category.Name);
            Console.WriteLine("属性：{0}", property.Name);
            Console.WriteLine("值：{0}", propertyValue.Name);
            this.BreakRow();
        }

        #endregion

        #region 商品信息采集的监视

        public void OverseeInsertProduct(Merchandise merchandise)
        {
            this.Oversee(merchandise, "操作：插入");
        }

        public void OverseeUpdateProduct(Merchandise merchandise)
        {
            this.Oversee(merchandise, "操作：更新");
        }

        public void Oversee(Merchandise merchandise, string action)
        {
            Console.WriteLine(action);
            Console.WriteLine("分类：{0}", merchandise.CatalogId);
            Console.WriteLine("名称：{0}", merchandise.Title);
            Console.WriteLine("价格：{0}", merchandise.Price);
            Console.WriteLine("商品所在地：{0}", merchandise.ItemLocation);
            Console.WriteLine("上架时间：{0}", merchandise.ListTime);
            List<ComplexProperty> results = Parser.Parse(merchandise.CatalogId, merchandise.Properties);
            foreach (var result in results)
            {
                if (result.Result != null)
                {
                    Console.WriteLine("{0}：{1}", result.Result.PropertyName, result.Result.Name);
                }
                else
                {
                    Console.WriteLine("{0}：{1}", result.Key, result.Value);
                }
            }
            Console.WriteLine(string.Format("{0}状态：成功", action));
            ImageHandler.DownloadPicture(merchandise, Console.WriteLine);
            ImageHandler.CreateThumbnail(merchandise, Console.WriteLine);
            this.BreakRow();
        }

        #endregion

        /// <summary>
        /// 输出格式化
        /// </summary>
        /// <param name="message">输出消息</param>
        private void BreakRow()
        {
            Console.WriteLine("——————————————");
        }
    }
}
